package com.lz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lz.service.CommonService;
import com.lz.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lz on 2018/5/18.
 */
@Controller
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private CommonService commonService;

    /**
     * 通过关键字查询
     * @param request
     */
    @RequestMapping("/keyword")
    @ResponseBody
    public JSONObject queryByKeyword(HttpServletRequest request){
        String keyword = request.getParameter("keyword");//关键字
        String product = request.getParameter("product");//产品类型
        String initInvestAmount = request.getParameter("amount");//投入金额
        String investPeriod = request.getParameter("investPeriod");//投资期
        String riskLevel = request.getParameter("level");//风险水平
        String pageSize = request.getParameter("pageSize");//每页显式多少条记录
        String pageIndex = request.getParameter("pageIndex");//当前页号
        String sortType = request.getParameter("sortType");//顺序
        String field = request.getParameter("field");//字段
        JSONObject object = null;
        try {
            keyword = (keyword == null) ? "" : keyword;
            initInvestAmount = (initInvestAmount == null) ? "" : initInvestAmount;
            investPeriod = (investPeriod == null) ? "" : investPeriod;
            riskLevel = (riskLevel == null) ? "" : riskLevel;
            pageSize = (pageSize == null) ? "" : pageSize;
            pageIndex = (pageIndex == null) ? "" : pageIndex;
            sortType = (sortType == null) ? "2" : sortType;
            field = (field == null) ? "1" : field;
            if (StringUtils.isNotBlank(product)){

                PageUtil pageUtil = commonService.queryByKeyword(keyword, product, initInvestAmount, investPeriod, riskLevel, pageSize, pageIndex,sortType,field);
                object = (JSONObject)JSON.toJSON(pageUtil);
            } else {
                object = new JSONObject();
                object.put("msg","没有产品类型");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    /**
     * 查询所有
     * @param request
     * @return
     */
    @RequestMapping("/all")
    @ResponseBody
    public List queryAll(HttpServletRequest request){
        String keyword = request.getParameter("keyword");//关键字
        keyword = (keyword == null) ? "" : keyword;
        return commonService.queryByKeyword1(keyword);
    }

    /**
     * 通过id查询产品信息
     * @param request
     */
    @RequestMapping("/info")
    @ResponseBody
    public List<Map<String, Object>> queryByProductId(HttpServletRequest request){
        String product = request.getParameter("product");
        String pId = request.getParameter("pId");
        List<Map<String, Object>> list = null;
        try {
            if (StringUtils.isBlank(product) || StringUtils.isBlank(pId)){
                return null;
            }
            list = commonService.queryByProductId(product,pId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
